<template>
  <div class="user-info" :model="userInfo">
    <!-- 个人信息 -->
    <el-form label-width="220px">
      <!-- 工号 入职时间 -->
      <el-row class="inline-info">
        <el-col :span="12">
          <el-form-item label="工号">
            <el-input v-model="userInfo.workNumber" class="inputW" />
          </el-form-item>
        </el-col>

        <el-col :span="12">
          <el-form-item label="入职时间">
            <el-date-picker
              v-model="userInfo.timeOfEntry"
              style="width: 300px"
              type="date"
              class="inputW"
            />
          </el-form-item>
        </el-col>
      </el-row>
      <!-- 姓名 部门 -->
      <el-row class="inline-info">
        <el-col :span="12">
          <el-form-item label="姓名">
            <el-input v-model="userInfo.username" class="inputW" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="部门" prop="departmentName">
            <el-input
              v-model="userInfo.departmentName"
              class="inputW"
              placeholder="请选择部门"
              @focus="showDeparOption"
              @input="deparInputFn"
            />
            <div v-show="isTreeShow" class="tree-box">
              <el-tree :data="deparTreeList" default-expand-all :props="{ label: 'name' }" @node-click="deparNodeClickFn" />
            </div>

          </el-form-item>
        </el-col>
      </el-row>
      <!--手机 聘用形式  -->
      <el-row class="inline-info">
        <el-col :span="12">
          <el-form-item label="手机">
            <el-input v-model="userInfo.mobile" style="width: 300px" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="聘用形式">
            <el-select v-model="userInfo.formOfEmployment" class="inputW">
              <el-option
                v-for="item in EmployeeEnum.hireType"
                :key="item.id"
                :label="item.value"
                :value="item.id"
              />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <!-- 员工照片 -->
      <el-row class="inline-info">
        <el-col :span="12">
          <el-form-item label="员工头像">
            <!-- 放置上传图片 -->
            <!-- v-model
            作用:实现双向数据绑定
            本质:
            给当前标签加:value 属性  v-model右侧变量赋予给value属性(数据->视图)
            给当前页面加@input 事件 并且把收到的值直接赋予v-model右边的变量(视图->数据) -->
            <!-- 和.sync 有点相似  但是.sync可以写多个 vue2.6版本 v-model只能写一个 (vue3 可以写多个)
            都是为了子组件快速给父组件赋值-->
            <UploadImg v-model="userInfo.staffPhoto" />

            <!-- <UploadImg :value="userInfo.staffPhoto" @input="v=>userInfo.staffPhoto=v" /> -->
          </el-form-item>
        </el-col>
      </el-row>
      <!-- 保存个人信息 -->
      <el-row class="inline-info" type="flex" justify="center">
        <el-col :span="12">
          <el-button type="primary" @click="saveEmp">保存更新</el-button>
          <el-button @click="$router.back()">返回</el-button>

        </el-col>
      </el-row>
    </el-form>
  </div>
</template>

<script>

import EmployeeEnum from '@/api/constant'

export default {
  props: {
    // 部门列表数据(树形结构)
    deparTreeList: {
      type: Array,
      default: () => []
    }
  },
  data() {
    return {
      // userId: this.$route.query.id,
      EmployeeEnum, // 员工枚举数据
      userInfo: {
        staffPhoto: '',
        username: '',
        workNumber: '',
        timeOfEntry: '',
        departmentName: '',
        mobile: '',
        formOfEmployment: ''
      },
      isTreeShow: false, // 控制部门选项出现
      deparOriginalName: ''
    }
  },

  created() {

  },
  methods: {

    saveEmp() {
      this.$emit('updateUserInfoEV', this.userInfo)
    },
    // 部门获取焦点显示选项
    showDeparOption() {
      this.isTreeShow = true
    },
    // 点击Tree 节点功能
    deparNodeClickFn(data) {
      if (data && !data.children) { // 没有子节点才赋予选中的部门名字给输入框
        this.userInfo.departmentName = data.name
        this.deparOriginalName = data.name // 因为formData里面的值被页面输入的值替换(丢失了最开始的部门名)
        this.isTreeShow = false
      }
    },
    // 部门输入框实时改变shijian
    deparInputFn() {
      this.userInfo.departmentName = this.deparOriginalName
    }
  }
}
</script>

<style scoped lang="scss">
  .user-info{
    padding-top:20px;
  }

</style>
